package com.googlecode.kipler.satisfiability.completion.strategy;

import java.util.Iterator;
import java.util.List;

import com.googlecode.kipler.container.dl.Individual;
import com.googlecode.kipler.container.dl.World;
import com.googlecode.kipler.container.dl.WorldConstraintSystem;
import com.googlecode.kipler.satisfiability.completion.rule.CompletionRule;
import com.googlecode.kipler.satisfiability.completion.rule.individual.ABoxRule;
import com.googlecode.kipler.satisfiability.reasoner.ReasonerException;


public class RuleApplication {
	private WorldConstraintSystem currentWcs;

	private World currentWorld;

	public RuleApplication(WorldConstraintSystem wcs, World world) {
		this.currentWcs = wcs;
		this.currentWorld = world;
	}

	public boolean applySingleFormulaRule(List<CompletionRule> ruleList)
			throws ReasonerException {
		boolean ruleApplied = false;
		Iterator<CompletionRule> ruleIter = ruleList.iterator();
		while (ruleIter.hasNext() && !ruleApplied) {
			CompletionRule rule = ruleIter.next();
			rule.setWcs(currentWcs);
			rule.setWorld(currentWorld);
			rule.apply();
			ruleApplied = rule.isApplied();
		}
		return ruleApplied;
	}

	public boolean applySingleIndividualRule(List<ABoxRule> ruleList,
			Individual indv) throws ReasonerException {
		boolean ruleApplied = false;
		Iterator<ABoxRule> ruleIter = ruleList.iterator();
		while (ruleIter.hasNext() && !ruleApplied) {
			ABoxRule rule = ruleIter.next();
			rule.setWcs(currentWcs);
			rule.setWorld(currentWorld);
			rule.setIndividual(indv);
			rule.apply();
			ruleApplied = rule.isApplied();
		}
		return ruleApplied;
	}
}
